/**
 * 表格的数据部分
 */
import {
  deepCopyObjArray,
  deepCopyObj
} from '../../assets/js/help.js';

export default {
  props: {
    isResetTable: {
      type: [Boolean, String],
      default: false
    }, //是否需要重置表格（即重新绑定固定表头插件）
    tableBoxId: { //表格的父元素的ID
      type: String,
      default: ''
    },
    hasCheckboxCol: {
      type: [Boolean, String],
      default: true
    },
    hasOperateCol: {
      type: [Boolean, String],
      default: true
    },
    isSetTable: {
      type: [Boolean, String],
      default: true
    },
    isNeedPrint: {
      type: [Boolean, String],
      default: false
    },
    theadsInfo: Array,
    tbodysInfo: Array,
    sortColumn: String,
    orderType: String,
    maxHeight: {
      type: Number,
      default: 0
    },
    tHeightModal: { // 弹框的表格的高度
      type: [Number, String],
      default: null,
    },
    fixedCols: { // 固定列的列数， 默认固定3列
      type: [Number, String],
      default: 3
    },
    isNeedTree: {
      type: [Boolean, String],
      default: false
    },
    selectedNode: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      tHeight: 500, //表格的高度，与固定表头相关，必须设置为Number格式
      isShowSetting: false, //是否显示表格设置框
      theadShowed: [], //表格的显示列，初始默认为父组件传过来的值
      theadsArr: [],
      orderBy: this.sortColumn,
      orderTy: this.orderType == 'ASC' ? 'ascending' : (this.orderType == 'DESC' ? 'descending' : ''),
      theadHidden: [], // 表格的隐藏列，初始为空
      multipleSelection: [], //表格的多选列表
      thisRouter: this.tableBoxId || this.$route.meta.name, // 当前的路由标签meta
      tableId: '',
      singleThWidth: 0,
      emptyText: '请输入搜索条件',
      isReload: false,
      isShowTree: true
    }
  },
  created() {
    let userLoginId = window.sessionStorage.getItem('loginid')
    this.tableId = userLoginId + '-table-cache';
    this.initFilterTheads();
  },
  watch: {
    'tbodysInfo': function (val, oldVal) {
      if (val.length == 0) {
        this.emptyText = '暂无数据';
      } else {
        if (oldVal.length == 0) {
          this.emptyText = '数据加载中...';
        }
      }
      if (this.isPrintNext) {
        this.toPrintTable();
        this.isPrintNext = false;
      }
    },

    isResetTable(Val) { //重绘表格
      this.isReload = true
      this.$nextTick(() => {
        this.isReload = false
      })
    },
  },
  methods: {
    //初始过滤 默认隐藏和显示的列
    initFilterTheads() {
      let list = this.theadsInfo
      let oldList = []
      let flag = false
      let showed = [];
      let hidden = [];
      let name = this.tableId;
      let key = this.thisRouter
      let _storageT = sessionStorage.getItem(name);
      _storageT = _storageT ? JSON.parse(_storageT) : []
      let _storage = _storageT.find(elem=>{
        return elem.key==key
      })
      if(_storage && _storage.hasOwnProperty('list') && _storage.list.length){
        flag = true
        oldList = _storage.list
      }
      for (let i = 0, len = list.length; i < len; i++) {
        let initHidden = list[i].initHidden;
        let _obj = deepCopyObj(list[i])
        let checked = false
        _obj.isShow = false

        if(flag){ //存在缓存
          let item1 = oldList.find(elem=>{
            return elem.prop==_obj.prop
          })
          let item2 = oldList.find(elem=>{
            return elem.prop=='!'+_obj.prop
          })
          if(item1){ //显示
            showed.push(_obj);
            checked = true
          }else if(item2){ //隐藏
            hidden.push(_obj);
          }else{ //不在缓存里
            if (initHidden) {
              hidden.push(_obj);
            } else {
              showed.push(_obj);
              checked = true
            }
          }
        }else{
          if (initHidden) {
            hidden.push(_obj);
          } else {
            showed.push(_obj);
            checked = true
          }
        }
        list[i]['checked'] = checked
        if (list[i].children && list[i].children.length > 0) {
          let children = list[i].children
          children.forEach(item => {
            item['checked'] = checked
          })
        }
      }
      this.theadsArr = list;
      this.theadShowed = deepCopyObjArray(showed);
      this.theadHidden = deepCopyObjArray(hidden);
      this.saveSetting();
    },
  }
}
